#include "io.h"

#ifndef PASSWORDBUFSIZE
#define PASSWORDBUFSIZE 101
#endif

static const char* users_db_path = "./users.db";
static const char* books_db_path = "./books.db";
static const char* recods_db_path = "./recods.db";

static const char* admin_password_file_path = "./admin.txt";

const char* get_users_db_path()
{
    return users_db_path;
}

const char* get_books_db_path()
{
    return books_db_path;
}

const char* get_recods_db_path()
{
    return recods_db_path;
}

FILE* initialize_file(const char* path)
{
    FILE* result = NULL;
    if((result = fopen(path, "r")) == NULL)
    {
        fclose(fopen(path, "w"));
    }
    return result;
}


int sync_cache(user* users_list, book* books_list, recod* recods_list)
{
    if(users_list != NULL)
    {
        FILE* users_db = fopen(users_db_path, "w");
        if(users_db != NULL)
        {
            user* current_user = users_list;
            do
            {
                fprintf(users_db, "%s$%s$%d$%d$%s\n", current_user->number, current_user->name, current_user->total, current_user->rest, current_user->password);
            }
            while((current_user = current_user->next) != NULL);
            fclose(users_db);
        }
        else
            return 0;
    }

    if(books_list != NULL)
    {
        FILE* books_db = fopen(books_db_path, "w");
        if(books_db != NULL)
        {
            book* current_book = books_list;
            do
            {
                fprintf(books_db, "%s$%s$%s$%s$%d$%d\n", current_book->isbn, current_book->title, current_book->author, current_book->press, current_book->total, current_book->rest);
            }
            while((current_book = current_book->next) != NULL);
            fclose(books_db);
        }
        else
            return 0;
    }

    if(recods_list != NULL)
    {
        FILE* recods_db = fopen(recods_db_path, "w");
        if(recods_db != NULL)
        {
            recod* current_recod = recods_list;
            do
            {
                char* temp_recod_indate;
                if(current_recod->indate == NULL)
                    temp_recod_indate = "N\\A";
                else
                    temp_recod_indate = current_recod->indate;
                fprintf(recods_db, "%s$%s$%s$%s$%s\n", current_recod->number, current_recod->book_num, current_recod->user_num, current_recod->outdate, temp_recod_indate);
            }
            while((current_recod = current_recod->next) != NULL);
            fclose(recods_db);
        }
        else
            return 0;
    }
    return 1;
}

char* get_admin_password()
{
    FILE* admin_password_file = fopen(admin_password_file_path, "r");
    if(admin_password_file == NULL)
        return NULL;
    char* result[PASSWORDBUFSIZE];
    if(fscanf(admin_password_file, "%s\n", &result) == 0)
    {
        fclose(admin_password_file);
        return NULL;
    }
    return strdup(result);
}

void set_admin_password(char* new_password)
{
    FILE* admin_password_file = fopen(admin_password_file_path, "w");
    fprintf(admin_password_file, "%s", new_password);
    fclose(admin_password_file);
}
